<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">




<script>
    (function(){
        if(''){
            if (prompt('请输入文章密码') !== ''){
                alert('密码错误！');
                history.back();
            }
        }
    })();
  </script>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />



  <link rel="icon" type="image/png" sizes="32x32" href="/favicon.ico?v=5.1.4">







  <meta name="keywords" content="Swift," />





  <link rel="alternate" href="/atom.xml" title="Swift社区" type="application/atom+xml" />






<meta name="description" content="前言模糊的数据可以说是一般应用程序中最常见的错误和问题的来源之一。虽然 Swift 通过其强大的类型系统和完善的编译器帮助我们避免了许多含糊不清的来源——但只要我们无法在编译时保证某个数据总是符合我们的要求，就总是有风险，我们最终会处于含糊不清或不可预测的状态。 本周，让我们来看看一种技术，它可以让我们利用 Swift 的类型系统在编译时执行更多种类的数据验证——消除更多潜在的歧义来源，并帮助我们">
<meta name="keywords" content="Swift">
<meta property="og:type" content="article">
<meta property="og:title" content="Swift 中的幻象类型">
<meta property="og:url" content="https://fanbaoying.github.io/Swift-中的幻象类型/index.html">
<meta property="og:site_name" content="Swift社区">
<meta property="og:description" content="前言模糊的数据可以说是一般应用程序中最常见的错误和问题的来源之一。虽然 Swift 通过其强大的类型系统和完善的编译器帮助我们避免了许多含糊不清的来源——但只要我们无法在编译时保证某个数据总是符合我们的要求，就总是有风险，我们最终会处于含糊不清或不可预测的状态。 本周，让我们来看看一种技术，它可以让我们利用 Swift 的类型系统在编译时执行更多种类的数据验证——消除更多潜在的歧义来源，并帮助我们">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2022-05-25T03:27:40.504Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Swift 中的幻象类型">
<meta name="twitter:description" content="前言模糊的数据可以说是一般应用程序中最常见的错误和问题的来源之一。虽然 Swift 通过其强大的类型系统和完善的编译器帮助我们避免了许多含糊不清的来源——但只要我们无法在编译时保证某个数据总是符合我们的要求，就总是有风险，我们最终会处于含糊不清或不可预测的状态。 本周，让我们来看看一种技术，它可以让我们利用 Swift 的类型系统在编译时执行更多种类的数据验证——消除更多潜在的歧义来源，并帮助我们">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://fanbaoying.github.io/Swift-中的幻象类型/"/>





  <title>Swift 中的幻象类型 | Swift社区</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    
    <a href="https://github.com/SwiftCommunityRes" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    
    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Swift社区</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">做最好的 Swift 社区</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://fanbaoying.github.io/Swift-中的幻象类型/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Swift社区">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://avatars.githubusercontent.com/u/84354365?v=4">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Swift社区">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Swift 中的幻象类型</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2022-05-25T11:22:44+08:00">
                2022-05-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Swift/" itemprop="url" rel="index">
                    <span itemprop="name">Swift</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                -->
                <span title="字数统计">
                  数字统计 2,919字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>

                <!-- 
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                --> 

                <span title="阅读时长">
                  阅读时长 11分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>模糊的数据可以说是一般应用程序中最常见的错误和问题的来源之一。虽然 Swift 通过其强大的类型系统和完善的编译器帮助我们避免了许多含糊不清的来源——但只要我们无法在编译时保证某个数据总是符合我们的要求，就总是有风险，我们最终会处于含糊不清或不可预测的状态。</p>
<p>本周，让我们来看看一种技术，它可以让我们利用 Swift 的类型系统在编译时执行更多种类的数据验证——消除更多潜在的歧义来源，并帮助我们在整个代码库中保持类型安全——通过使用<strong>幻象类型</strong>(<em>phantom types</em>)。</p>
<a id="more"></a>
<h2 id="定义良好，但仍然含糊不清"><a href="#定义良好，但仍然含糊不清" class="headerlink" title="定义良好，但仍然含糊不清"></a>定义良好，但仍然含糊不清</h2><p>举个例子，假设我们正在开发一个文本编辑器，虽然它最初只支持纯文本文件——随着时间的推移，我们还增加了对编辑HTML文档的支持，以及PDF预览。</p>
<p>为了能够尽可能多地重复使用我们原来的文档处理代码，我们继续使用与开始时相同的<code>Document</code>模型——只是现在它获得了一个<code>Format</code>属性，告诉我们正在处理什么样的文档：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Document</span> </span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">Format</span> </span>&#123;</span><br><span class="line">        <span class="keyword">case</span> text</span><br><span class="line">        <span class="keyword">case</span> html</span><br><span class="line">        <span class="keyword">case</span> pdf</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> format: <span class="type">Format</span></span><br><span class="line">    <span class="keyword">var</span> data: <span class="type">Data</span></span><br><span class="line">    <span class="keyword">var</span> modificationDate: <span class="type">Date</span></span><br><span class="line">    <span class="keyword">var</span> author: <span class="type">Author</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>能够避免代码重复当然是件好事，而且枚举是当我们在处理一个模型的不同格式或变体时<a href="https://www.swiftbysundell.com/articles/modelling-state-in-swift" target="_blank" rel="noopener">一般情况下建模</a> 的好方法，但是上述那种设置实际上最终会造成相当多的模糊性。</p>
<p>例如，我们可能有一些API，只有在调用给定格式的文档时才有意义——比如这个打开文本编辑器的函数，它假定任何传入它的<code>Document</code>都是文本文档：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">openTextEditor</span><span class="params">(<span class="keyword">for</span> document: Document)</span></span> &#123;</span><br><span class="line">    <span class="keyword">let</span> text = <span class="type">String</span>(decoding: document.data, <span class="keyword">as</span>: <span class="type">UTF8</span>.<span class="keyword">self</span>)</span><br><span class="line">    <span class="keyword">let</span> editor = <span class="type">TextEditor</span>(text: text)</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>虽然如果我们不小心将一个HTML文档传递给上述函数并不是世界末日（HTML毕竟只是文本），但试图以这种方式打开一个PDF，很可能会导致呈现出完全无法理解的东西，我们的文本编辑功能将无法工作，我们的应用程序甚至可能最终崩溃。</p>
<p>我们在编写任何其他特定格式的代码时都会不断遇到同样的问题，例如，如果我们想通过实现一个解析器和一个专门的编辑器来改善编辑HTML文档的用户体验：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">openHTMLEditor</span><span class="params">(<span class="keyword">for</span> document: Document)</span></span> &#123;</span><br><span class="line">    <span class="comment">// 就像我们上面用于文本编辑的函数一样，</span></span><br><span class="line">    <span class="comment">// 这个函数假设它总是被传递给HTML文档。</span></span><br><span class="line">    <span class="keyword">let</span> parser = <span class="type">HTMLParser</span>()</span><br><span class="line">    <span class="keyword">let</span> html = parser.parse(document.data)</span><br><span class="line">    <span class="keyword">let</span> editor = <span class="type">HTMLEditor</span>(html: html)</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>一个关于如何解决上述问题的初步想法可能是编写一个包装函数，切换到所传递文档的格式，然后为每种情况打开正确的编辑器。然而，虽然这对文本和HTML文档很有效，但由于PDF文档在我们的应用程序中是不可编辑的——当遇到PDF时，我们将被迫抛出一个错误，触发一个断言，或以<a href="https://www.swiftbysundell.com/articles/picking-the-right-way-of-failing-in-swift" target="_blank" rel="noopener">其他方式失败</a>：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">openEditor</span><span class="params">(<span class="keyword">for</span> document: Document)</span></span> &#123;</span><br><span class="line">    <span class="keyword">switch</span> document.format &#123;</span><br><span class="line">    <span class="keyword">case</span> .text:</span><br><span class="line">        openTextEditor(<span class="keyword">for</span>: document)</span><br><span class="line">    <span class="keyword">case</span> .html:</span><br><span class="line">        openHTMLEditor(<span class="keyword">for</span>: document)</span><br><span class="line">    <span class="keyword">case</span> .pdf:</span><br><span class="line">        <span class="built_in">assertionFailure</span>(<span class="string">"Cannot edit PDF documents"</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述情况不是很好，因为它要求我们作为开发者始终跟踪我们在任何给定的代码路径中所处理的文件类型，而我们可能犯的任何错误只能在运行时被发现——编译器根本没有足够的信息可以在编译时进行这种检查。</p>
<p>因此，尽管我们的 “Document “模型乍一看可能非常优雅和完善，但事实证明，它并不完全是手头情况的正确解决方案。</p>
<h2 id="看起来我们需要一个协议"><a href="#看起来我们需要一个协议" class="headerlink" title="看起来我们需要一个协议!"></a>看起来我们需要一个协议!</h2><p>解决上述问题的一个方法是把<code>Document</code>变成一个协议，而不是作为一个具体的类型，把它的所有属性（除了<code>format</code>）都作为要求：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">protocol</span> <span class="title">Document</span> </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> data: <span class="type">Data</span> &#123; <span class="keyword">get</span> &#125;</span><br><span class="line">    <span class="keyword">var</span> modificationDate: <span class="type">Date</span> &#123; <span class="keyword">get</span> &#125;</span><br><span class="line">    <span class="keyword">var</span> author: <span class="type">Author</span> &#123; <span class="keyword">get</span> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>有了上述变化，我们现在可以为我们的三种文档格式中的每一种实现专门的类型，并让这些类型都符合我们新的文档协议——比如这样：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">TextDocument</span>: <span class="title">Document</span> </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> data: <span class="type">Data</span></span><br><span class="line">    <span class="keyword">var</span> modificationDate: <span class="type">Date</span></span><br><span class="line">    <span class="keyword">var</span> author: <span class="type">Author</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上述方法的好处是，它使我们既能实现可以对任何<code>Document</code>进行操作的通用功能，又能实现只接受某种具体类型的特定API：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这个函数可以保存任何文件，</span></span><br><span class="line"><span class="comment">// 所以它接受任何符合我们的新文档协议。</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">save</span><span class="params">(<span class="number">_</span> document: Document)</span></span> &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 我们现在只能向我们的函数传递文本文件，</span></span><br><span class="line"><span class="comment">// 即打开一个文本编辑器。</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">openTextEditor</span><span class="params">(<span class="keyword">for</span> document: TextDocument)</span></span> &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们在上面所做的基本上是将以前在运行时进行的检查转为在编译时进行验证——因为编译器现在能够检查我们是否总是向我们的每个API传递正确格式的文件，这是一个很大的进步。</p>
<p>然而，通过执行上述改变，我们也<strong>失去了我们最初实现的优点——代码重用</strong>。由于我们现在使用一个协议来表示所有的文档格式，我们将需要为我们的三种文档类型中的每一种编写完全重复的模型实现，以及为我们将来可能增加的任何其他格式提供支持。</p>
<h2 id="引入幻象类型"><a href="#引入幻象类型" class="headerlink" title="引入幻象类型"></a>引入幻象类型</h2><p>如果我们能找到一种方法，既能为所有格式重用相同的<code>Document</code>模型，又能在编译时验证我们特定格式的代码，岂不妙哉？事实证明，我们之前的一行代码实际上可以给我们一个实现这一目标的提示：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> text = <span class="type">String</span>(decoding: document.data, <span class="keyword">as</span>: <span class="type">UTF8</span>.<span class="keyword">self</span>)</span><br></pre></td></tr></table></figure>
<p>当把<code>Data</code>转换为<code>String</code>时，就像我们上面做的那样，我们通过传递对该类型本身的引用来传递我们希望字符串被解码的编码——在本例中是UTF8。这真的很有趣。如果我们再深入一点，就会发现 Swift 标准库将我们上面提到的UTF8类型定义为另一个类似命名空间的枚举中的一个无大小写枚举，称为<code>Unicode</code>。</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">Unicode</span> </span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">UTF8</span> </span>&#123;&#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">typealias</span> <span class="type">UTF8</span> = <span class="type">Unicode</span>.<span class="type">UTF8</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>请注意，如果你看一下<code>UTF8</code>类型的实际实现，它确实包含一个私有case，只是为了向后兼容 Swift 3 而存在。</p>
</blockquote>
<p>我们在这里看到的是一种被称为<strong>幻象类型的技术——当类型被用作标记，而不是被实例化来表示值或对象时</strong>。事实上，由于上述枚举都没有任何公开的情况，它们甚至不能被实例化！</p>
<p>让我们看看是否可以用同样的技术来解决我们的<code>Document</code>困境。我们首先将<code>Document</code>还原成一个结构体，只是这次我们将删除它的<code>format</code>属性（以及相关的枚举），而将它变成一个覆盖任何<code>Format</code>类型的泛型——比如这样:</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Document</span>&lt;<span class="title">Format</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> data: <span class="type">Data</span></span><br><span class="line">    <span class="keyword">var</span> modificationDate: <span class="type">Date</span></span><br><span class="line">    <span class="keyword">var</span> author: <span class="type">Author</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>受标准库的<code>Unicode</code>枚举及其各种编码的启发，我们将定义一个类似的枚举——<code>DocumentFormat</code>——作为三个无大小写的枚举的命名空间，每种格式都有一个：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">enum</span> <span class="title">DocumentFormat</span> </span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">Text</span> </span>&#123;&#125;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">HTML</span> </span>&#123;&#125;</span><br><span class="line">    <span class="class"><span class="keyword">enum</span> <span class="title">PDF</span> </span>&#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>请注意，这里不涉及任何协议——任何类型都可以被用作格式，因为就像<code>String</code>和它的各种编码一样，我们将只使用文档的<code>Format</code>类型作为编译时的标记。这将使我们能够像这样写出我们特定格式的API：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">openTextEditor</span><span class="params">(<span class="keyword">for</span> document: Document&lt;DocumentFormat.Text&gt;)</span></span> &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">openHTMLEditor</span><span class="params">(<span class="keyword">for</span> document: Document&lt;DocumentFormat.HTML&gt;)</span></span> &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">openPreview</span><span class="params">(<span class="keyword">for</span> document: Document&lt;DocumentFormat.PDF&gt;)</span></span> &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当然，我们仍然可以编写不需要任何特定格式的通用代码。例如，这里我们可以把之前的<code>save</code>API变成一个完全通用的函数：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">func</span> <span class="title">save</span>&lt;F&gt;<span class="params">(<span class="number">_</span> document: Document&lt;F&gt;)</span></span> &#123;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然而，总是输入<code>Document&lt;DocumentFormat.Text&gt;</code>来引用一个文本文档是相当乏味的，所以让我们也使用类型别名为每种格式定义速记。这将给我们提供漂亮的、有语义的名字，而不需要任何重复的代码：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typealias</span> <span class="type">TextDocument</span> = <span class="type">Document</span>&lt;<span class="type">DocumentFormat</span>.<span class="type">Text</span>&gt;</span><br><span class="line"><span class="keyword">typealias</span> <span class="type">HTMLDocument</span> = <span class="type">Document</span>&lt;<span class="type">DocumentFormat</span>.<span class="type">HTML</span>&gt;</span><br><span class="line"><span class="keyword">typealias</span> <span class="type">PDFDocument</span> = <span class="type">Document</span>&lt;<span class="type">DocumentFormat</span>.<span class="type">PDF</span>&gt;</span><br></pre></td></tr></table></figure>
<p>在涉及到特定格式的扩展时，幻象类型也确实大放异彩，现在可以直接使用 Swift 强大的泛型系统和<a href="https://www.swiftbysundell.com/articles/using-generic-type-constraints-in-swift-4/" target="_blank" rel="noopener">泛型型约束</a>来实现。例如，我们可以用一个生成<code>NSAttributedString</code>的方法来扩展所有文本文档:</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">extension</span> <span class="title">Document</span> <span class="title">where</span> <span class="title">Format</span> == <span class="title">DocumentFormat</span>.<span class="title">Text</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">makeAttributedString</span><span class="params">(withFont font: UIFont)</span></span> -&gt; <span class="type">NSAttributedString</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> string = <span class="type">String</span>(decoding: data, <span class="keyword">as</span>: <span class="type">UTF8</span>.<span class="keyword">self</span>)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="type">NSAttributedString</span>(string: string, attributes: [</span><br><span class="line">            .font: font</span><br><span class="line">        ])</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于我们的幻象类型在最后只是普通的类型——我们也可以让它们遵守协议，并使用这些协议作为泛型约束。例如，我们可以让我们的一些<code>DocumentFormat</code>类型遵守<code>Printable</code>协议，然后我们可以在打印代码中使用这些协议作为约束条件。这里有大量的可能性。</p>
<h2 id="一个标准的模式"><a href="#一个标准的模式" class="headerlink" title="一个标准的模式"></a>一个标准的模式</h2><p>起初，幻象类型在 Swift 中可能看起来有点 “格格不入”。然而，虽然 Swift 并没有像更多的纯函数式语言（如Haskell）那样为幻象类型提供一流的支持，但在标准库和苹果平台SDK的许多不同地方都可以找到这种模式。</p>
<p>例如，<code>Foundation</code>的<code>Measurement</code> API使用幻象类型来确保在传递各种测量值时的类型安全——例如度数、长度和重量：</p>
<figure class="highlight swift"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> meters = <span class="type">Measurement</span>&lt;<span class="type">UnitLength</span>&gt;(value: <span class="number">5</span>, unit: .meters)</span><br><span class="line"><span class="keyword">let</span> degrees = <span class="type">Measurement</span>&lt;<span class="type">UnitAngle</span>&gt;(value: <span class="number">90</span>, unit: .degrees)</span><br></pre></td></tr></table></figure>
<p>通过使用幻影类型，上述两个测量值不能被混合，因为每个值是哪种单位，都被编码到该值的类型中。这可以防止我们不小心将一个长度传递给一个接受角度的函数，反之亦然——就像我们之前防止文档格式被混淆一样。</p>
<h2 id="结论"><a href="#结论" class="headerlink" title="结论"></a>结论</h2><p>使用幻象类型是一种非常强大的技术，它可以让我们利用类型系统来验证一个特定值的不同变体。虽然使用幻象类型通常会使API更加冗长，而且确实伴随着泛型的复杂性——当处理不同的格式和变体时，它可以让我们减少对运行时检查的依赖，而让编译器来执行这些检查。</p>
<p>就像一般的泛型一样，我认为在部署幻象类型之前，首先要仔细评估当前的情况，这很重要。就像我们最初的<code>Document</code>模型并不是手头任务的正确选择，尽管它的结构很好，但如果部署在错误的情况下，幻象类型会使简单的设置变得更加复杂。像往常一样，它归结为为工作选择正确的工具。</p>
<p>谢谢你的阅读! 🚀</p>

      
    </div>
    
    
    
    

    
      <div>
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      </div>
    


    
      <div>
        
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <p><span>本文标题:</span><a href="/Swift-中的幻象类型/">Swift 中的幻象类型</a></p>
  <p><span>文章作者:</span><a href="/" title="访问 Swift社区 的个人博客">Swift社区</a></p>
  <p><span>发布时间:</span>2022年05月25日 - 11:05</p>
  <p><span>最后更新:</span>2022年05月25日 - 11:05</p>
  <p><span>原始链接:</span><a href="/Swift-中的幻象类型/" title="Swift 中的幻象类型">https://fanbaoying.github.io/Swift-中的幻象类型/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="https://fanbaoying.github.io/Swift-中的幻象类型/"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
      $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
        });
    });  
</script>

      </div>
    


    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.png" alt="Swift社区 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.png" alt="Swift社区 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Swift/" rel="tag"><i class="fa fa-tag"></i> Swift</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/SwiftUI-动画进阶-—-Part-5：Canvas/" rel="next" title="SwiftUI 动画进阶 — Part 5：Canvas">
                <i class="fa fa-chevron-left"></i> SwiftUI 动画进阶 — Part 5：Canvas
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/LeetCode-31-下一个排列-（Top-100）/" rel="prev" title="LeetCode - 31 下一个排列 （Top 100）">
                LeetCode - 31 下一个排列 （Top 100） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript" >
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://avatars.githubusercontent.com/u/84354365?v=4"
                alt="Swift社区" />
            
              <p class="site-author-name" itemprop="name">Swift社区</p>
              <p class="site-description motion-element" itemprop="description">我们的使命是做一个最专业最权威的 Swift 中文社区，我们的愿景是希望更多的人学习和使用Swift。我们会分享以 Swift 实战、SwiftUI、Swift 基础为核心的技术干货，不忘初心，牢记使命。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">187</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_36478920" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/5a30d987f265da430d580126" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-spinner"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/fanbaoying" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/swift-community" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/7711465033" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i>微博</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://mp.weixin.qq.com/mp/appmsgalbum?__biz=MzAxNzgzNTgwMw==&action=getalbum&album_id=2024314764775981057" target="_blank" title="公众号">
                      
                        <i class="fa fa-fw fa-heartbeat"></i>公众号</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftWeekly" title="SwiftWeekly" target="_blank">SwiftWeekly</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/article-ios" title="Article-ios" target="_blank">Article-ios</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/SwiftCommunityRes/SwiftUI-Book" title="SwiftUI-Book" target="_blank">SwiftUI-Book</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#定义良好，但仍然含糊不清"><span class="nav-number">2.</span> <span class="nav-text">定义良好，但仍然含糊不清</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#看起来我们需要一个协议"><span class="nav-number">3.</span> <span class="nav-text">看起来我们需要一个协议!</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#引入幻象类型"><span class="nav-number">4.</span> <span class="nav-text">引入幻象类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一个标准的模式"><span class="nav-number">5.</span> <span class="nav-text">一个标准的模式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#结论"><span class="nav-number">6.</span> <span class="nav-text">结论</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="copyright">&copy; <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Swift社区</span>

  
</div>

<!-- 
<div class="powered-by">
  <i class="fa fa-user-md">
  </i>
  <span id="busuanzi_container_site_uv">
    已有<span id="busuanzi_value_site_uv"></span>人来访
  </span>

</div>


  <span class="post-meta-divider">|</span>


<div class="powered-by">
  <i class="fa fa-eye"></i>
  <span id="busuanzi_container_site_pv">
      访问量<span id="busuanzi_value_site_pv"></span>次
  </span>
</div>



  <span class="post-meta-divider">|</span>


-->

<div class="theme-info">
  <i class="fa fa-pencil"></i>
  <span class="post-count">博客全站共361.6k字</span>
</div>


<!-- 
  <span class="post-meta-divider">|</span>



  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>



-->

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
